package com.server.core.filter;

import com.server.core.domain.Executor;
import com.server.core.domain.Request;
import com.server.core.domain.Response;
import com.server.core.exception.FilterNotPassException;

/**
 * @desc 自定义过滤器，基于责任链 + 模版模式
 */
public abstract class AbstractFilter {

    AbstractFilter next;

    public void next(AbstractFilter next) {
        this.next = next;
    }

    /**
     * @desc 定义链式调用模版，如果需要过滤请求那么抛出异常，中断执行
     * @param request
     * @param response
     * @throws Exception
     */
    public void doChain(Request request, Response response) throws Exception {
       boolean isPassFilter = doFilter(request, response);
       if(!isPassFilter) {
           throw new FilterNotPassException();
       }
        if (next != null) {
            next.doChain(request, response);
        }
    }

    /**
     * @desc 过滤器核心抽象方法
     * @param request
     * @param response
     * @return
     */
    public abstract boolean doFilter(Request request, Response response) ;

}
